package com.junzhiit.nengji.app.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.GoodsService;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.GoodsAddVO;
import com.junzhiit.nengji.app.web.vo.GoodsEditVO;
import com.junzhiit.nengji.app.web.vo.GoodsListVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;

	@ApiOperation(value = "添加商品")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(GoodsAddVO goodsAddVO) {
		AssertUtil.notBlank(goodsAddVO.getName(),"name is required.");
		AssertUtil.notNull(goodsAddVO.getPrice(), "price is required.");
		AssertUtil.notNull(goodsAddVO.getType(), "type is required.");
		AssertUtil.notNull(goodsAddVO.getSystemTypeId(),"sessionId is required.");
		AssertUtil.notBlank(goodsAddVO.getTypeId(),"TypeId is required.");
		AssertUtil.notBlank(goodsAddVO.getSessionId(), "sessionId is required.");
		return goodsService.add(goodsAddVO);
	}

	@ApiOperation(value = "编辑商品")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO edit(GoodsEditVO goodsEditVO) {
		AssertUtil.notBlank(goodsEditVO.getId(), "id is required.");

		return goodsService.edit(goodsEditVO);
	}

	@ApiOperation(value = "查询所有商品")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findPageAll(DataTablesParamVO dtp, GoodsListVO goodsListVO) {
		return goodsService.findPageAll(dtp, goodsListVO);
	}

	@ApiOperation(value = "查询商品")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String id) {
		return goodsService.findById(id);
	}

	@ApiOperation(value = "查询商品(轮播.详情图等)")
	@RequestMapping(value = "/other/{id}", method = RequestMethod.GET)
	public ResultVO findByIdForOther(@PathVariable String id) {
		AssertUtil.notBlank(id, "id is required.");
		return goodsService.findByIdForOther(id);
	}

	@ApiOperation(value = "批量更新状态")
	@RequestMapping(value = "batch/status", method = RequestMethod.PUT)
	public ResultVO batchUpdateStatus(String[] ids, Integer status,String sessionId) {
		return goodsService.batchUpdateStatus(ids, status,sessionId);
	}

	@ApiOperation(value = "更新状态/上架/下架/申请在审核")
	@RequestMapping(value = "status", method = RequestMethod.PUT)
	public ResultVO updateStatus(String id, Integer status,String sessionId) {
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		AssertUtil.notNull(status,"status is required.");
		return goodsService.updateStatus(id, status,sessionId);
	}

	@ApiOperation(value="商户模拟删除")
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public ResultVO delete(@PathVariable String id){
		AssertUtil.notBlank(id, "id is required.");
		return goodsService.delete(id);
	}
	
	@ApiOperation(value="商户批量模拟删除")
	@RequestMapping(value="/batch/delete",method=RequestMethod.DELETE)
	public ResultVO batchDeleteChainGoods(String[] ids){
		return goodsService.batchDeleteChainGoods(ids);
	}
	
	
	@RequestMapping(value="program",method=RequestMethod.GET)
	public Map<String, Object> findByProgram(DataTablesParamVO dtp, GoodsListVO goodsListVO){
		return goodsService.findByProgram(dtp, goodsListVO);
	}
	
}
